package com.example.user.controller;

import com.example.common.api.ResultUtil;
import com.example.common.validator.Insert;
import com.example.common.validator.Update;
import com.example.user.model.vo.UserVO;
import com.example.user.service.impl.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/user")
@Slf4j
@Validated
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/create_user", method = RequestMethod.PUT)
    @ResponseBody
    public ResultUtil createUser(@RequestBody @Validated(Insert.class) UserVO userVO) {
        return userService.createUser(userVO);
    }

    @RequestMapping(value = "/update_user", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil updateUser(@RequestBody @Validated(Update.class) UserVO userVO){
        return userService.updateUser(userVO);
    }

    @RequestMapping(value = "/get_user", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getUser(@NotBlank(message = "账号不能为空") @Pattern(regexp = "^1\\d{10}$")
                                  @RequestParam("mobile") String mobile,
                              @RequestParam("password") String password) {
        return userService.getUser(mobile, password);
    }

    @RequestMapping(value = "/get_user_test", method = RequestMethod.GET)
    @ResponseBody
    public Mono<ResultUtil> getUserTest() {
        return Mono.just(userService.getUser("13641203361", "123456"));
    }
}
